//
//  ZGLTopViewController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/10/18.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "ZGLTopViewController.h"
#import "ZGLScoreBoardViewController.h"
#import "ZGLMatchsViewController.h"
#import "ZGLNewsViewController.h"
#import "LYDSegmentController.h"
#import "ZGLBaseModel.h"

@interface ZGLTopViewController ()

@property (nonatomic, strong) LYDSegmentController *segmentVC;
@property (nonatomic, strong) NSArray *controllersArray;
@property (nonatomic, strong) NSArray *titlesArray;
@property (nonatomic, strong) ZGLBaseModel *model;
@property (nonatomic, strong) ZGLNewsBannerImageModel *imageModel;
@property (nonatomic, strong) ZGLNewsBannerMatchModel *matchModel;

@property (nonatomic, assign) BOOL isImportant;
@property (nonatomic, strong) NSArray *matchListArr;
@property (nonatomic, strong) NSArray *imageListArr;


@end

@implementation ZGLTopViewController

- (NSArray *)matchListArr
{
    if (!_matchListArr) {
        _matchListArr = [NSArray new];
    }
    return _matchListArr;
}

-(NSArray *)imageListArr
{
    if (!_imageListArr) {
        _imageListArr = [NSArray new];
    }
    return _imageListArr;
}

- (NSArray *)controllersArray
{
    if (!_controllersArray) {
        _controllersArray = [NSArray new];
    }
    return _controllersArray;
}

- (NSArray *)titlesArray
{
    if (!_titlesArray) {
        _titlesArray = [NSArray new];
    }
    return _titlesArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"博猫体育";
    self.view.backgroundColor = ViewBackgroundColor;
    
    
    [self getMainPageData];
}

//获取数据初始化新闻页
- (void)getMainPageData
{
    
    DLog(@"befor-%@",self.titlesArray);
    
    [sZGLHTTPRequestManager getMainPageDataSuccess:^(id responsedata) {
        
        DLog(@"%@",responsedata);
        _model = [ZGLBaseModel mj_objectWithKeyValues:responsedata];
        self.isImportant = _model.importantSwitch;
        
        NSArray *matchArr = responsedata[@"matchList"];
        _matchListArr = [ZGLNewsBannerMatchModel mj_objectArrayWithKeyValuesArray:matchArr];
        NSArray *imageArr = responsedata[@"bannerList"];
        _imageListArr = [ZGLNewsBannerImageModel mj_objectArrayWithKeyValuesArray:imageArr];
        
        if (self.isImportant) {
            
            NSString *firstStr = self.model.importantTitle;
//            NSString *newsStr = @"新闻";
//            NSString *matchStr = @"赛事";
//            NSString *scoreBStr = @"数据";
//            [self.titlesArray insertObject:firstStr atIndex:0];
            _titlesArray = @[firstStr,@"新闻",@"赛事",@"数据"];
        }else{
            _titlesArray = @[@"新闻",@"赛事",@"数据"];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self addControllers];
        });
        
        
    } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
        
    }];
    
    
}

- (void)addControllers
{
    //导航栏适配
    self.navigationController.navigationBar.translucent = NO;
    
    ZGLNewsViewController *NewsVC = [[ZGLNewsViewController alloc]init];
    NewsVC.imageArr = self.imageListArr;
    NewsVC.matchArr = self.matchListArr;
    ZGLScoreBoardViewController *ScoreBoardVC = [[ZGLScoreBoardViewController alloc]init];
    ZGLMatchsViewController *MatchsVC = [[ZGLMatchsViewController alloc]init];
    ZGLMainViewController *mainVC = [[ZGLMainViewController alloc]init];
    
//    _controllersArray = @[mainVC,NewsVC,MatchsVC,ScoreBoardVC];@"新闻",@"赛事",@"数据"
    if (self.isImportant) {
        /*
         @[mainVC,NewsVC,MatchsVC,ScoreBoardVC]
         */
        _controllersArray = @[mainVC,NewsVC,MatchsVC,ScoreBoardVC];
        
        
        //左上个人侧栏
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"me_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController)];
        
    }else{
        
        _controllersArray = @[NewsVC,MatchsVC,ScoreBoardVC];
    }
    DLog(@"_controllersArray-%@",_controllersArray);
    
    self.segmentVC = [[LYDSegmentController alloc]initWithFrame:self.view.bounds titles:self.titlesArray];
    self.segmentVC.segmentView.segmentTintColor = globalRed;
    self.segmentVC.viewControllers = _controllersArray.copy;
    [self addSegmentController:_segmentVC];
    [self.segmentVC setSelectedAtIndex:0];
    
    
//    //右滑手势
//    UISwipeGestureRecognizer *recognizer;
//    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFromleft:)];
//    [recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
//    [self.view addGestureRecognizer:recognizer];
    
}

////右滑手势
//- (void)handleSwipeFromleft:(UISwipeGestureRecognizer *)gesture
//{
//    DLog(@"swipe right");
//    //    [self registerSlideToleft];
//    [self presentLeftMenuViewController];
//}

//跳转抽屉
- (void)presentLeftMenuViewController
{
    ZGLLeftMenuController *leftVc = [[ZGLLeftMenuController alloc]init];
    [self cw_showDrawerViewController:leftVc animationType:CWDrawerAnimationTypeMask configuration:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
